package com.wishwall.web.filters;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.wishwall.persistence.PersistenceContext;
import com.wishwall.web.utils.RequestHelper;

/**
 * 
 * @author yavor.gologanov
 *
 */
public class PersistenceContextFilter extends AbstractFilter {

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
			throws IOException, ServletException {
		
		PersistenceContext persistenceContext = null;
		try {
			persistenceContext = PersistenceContext.getPersistenceContext();			
			RequestHelper.setPersistenceContext(request, persistenceContext);
			
			doNextFilter(request, response, chain);
		} 
		finally {
			if (persistenceContext != null) {
				persistenceContext.close();
			}
		}
		
	}

}
